<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="模式名称">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.name"
                            placeholder="请输入模式名称"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-button
                type="primary"
                @click="emits('listen', 'editor', 0)"
                v-perms="['apply:docs:prompt:add']"
            >
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                <span>新增</span>
            </el-button>
            <el-table :data="pager.lists" size="large" class="mt-4">
                <el-table-column label="ID" prop="id" width="100" />
                <el-table-column label="图标" prop="icon" min-width="100">
                    <template #default="{ row }">
                        <el-image
                            :src="row.icon"
                            :lazy="true"
                            class="w-[30px] h-[30px]"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="名称" prop="name" min-width="100" />
                <el-table-column label="描述" prop="intro" min-width="100" />
                <el-table-column label="排序" prop="sort" width="80" />
                <el-table-column label="来源" prop="is_builtin" width="80">
                    <template #default="{ row }">
                        <el-tag type="primary" v-if="row.is_builtin === 1">内置</el-tag>
                        <el-tag type="info" v-else>自定</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="is_disable" width="80">
                    <template #default="{ row }">
                        <el-tag type="info" v-if="row.is_disable === 1">禁用</el-tag>
                        <el-tag type="success" v-else>正常</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" width="180" />
                <el-table-column label="操作" width="150" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['apply:docs:prompt:edit']"
                            type="primary"
                            link
                            @click="emits('listen', 'editor', row.id)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['apply:docs:prompt:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import docsPromptApi from '@/api/apply/docs/prompt'

const emits = defineEmits(['listen'])

// 查询参数
const queryParams = reactive({
    name: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: docsPromptApi.lists,
    params: queryParams
})

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.confirm('确定要删除此项数据吗?')
        .then(async () => {
            await docsPromptApi.delete(id)
            feedback.msgSuccess('删除成功')
            await queryLists()
        }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
})
</script>
